#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        return max(rot(nums, 0, n - 2), rot(nums, 1, n - 1));
    }
    int rot(vector<int>& nums, int l, int r)
    {
        if (l > r) return nums[0];
        int n = nums.size();
        vector<int>f(n);
        auto g = f;
        f[l] = nums[l];
        for (int i = l + 1; i <= r; i++)
        {
            f[i] = nums[i] + g[i - 1];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[r], g[r]);
    }
};